package com.miyu.system.service.openSystem.impl;

import com.alibaba.fastjson.JSONObject;
import com.miyu.common.dao.commonDAO.book.BookRuleSetDao;
import com.miyu.common.pojo.book.BookRuleSet;
import com.miyu.system.service.openSystem.OpenSysBookService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * impl sys书服务
 *
 * @author longm
 * @date 2022/09/13
 */
@Service
public class OpenSysBookServiceImpl implements OpenSysBookService {
    @Resource
    private BookRuleSetDao bookRuleSetDao;


    @Override
    @Cacheable(cacheNames = "bookRuleSet" ,key = "#params.containsKey('ruleId')?#params.getString('ruleId'):'all'")
    public List<BookRuleSet> queryBookRule(JSONObject params) {
        String ruleId="ruleId";
        List<BookRuleSet> bookRuleSet=new ArrayList<>();
        Sort sort = Sort.by(Sort.Direction.ASC,"ruleId");
        if (params.containsKey(ruleId)){
            List ruleIds = new ArrayList();
            ruleId=params.getString(ruleId);
            ruleIds.add(ruleId);
            if(ruleIds.size()>0) {
                bookRuleSet = bookRuleSetDao.findAllById(ruleIds);
            }
        }else {
            bookRuleSet = bookRuleSetDao.findAll(sort);
        }
        return bookRuleSet;
    }
}
